//
//  MSMyFollowViewController.m
//  SocietySale
//
//  Created by yumingwei on 2016/11/26.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSMyFollowViewController.h"
#import "MSMyFollowTableVC.h"
#import "MSProjectDetailViewController.h"

@interface MSMyFollowViewController ()
{
    NSString *currentProviceStr;
    NSString *currentCityStr;
    NSString *currentIndustryStr;
    NSString *currentStateStr;
    NSString *currentInvetStr;
    NSString *currentTimeStr;
    NSString *documentsPath;
    NSFileManager* fileManager;
}
@property (nonatomic, strong) MSMyFollowTableVC *tableVC;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, assign) float positionY;
@property (nonatomic, strong) HDImageMenuView     *menu;
@property (nonatomic, strong) MSDoubleListFilterVC              *areaFilterVC;       //地区筛选
@property (nonatomic, strong) MSSaleFilterViewController        *industryFilterVC;   //行业筛选
@property (nonatomic, strong) MSSaleFilterViewController        *stateFilterVC;      //阶段筛选
@property (nonatomic, strong) MSSaleFilterViewController        *investFilterVC;     //投资筛选
@property (nonatomic, strong) MSSaleFilterViewController        *timeFilterVC;       //时间筛选
@property (nonatomic, strong) MSSaleFilterViewController        *filterVC;           //筛选控件
@property (nonatomic, strong) MSSelectTimeView                  *selectTimeView;     //选择时间
@property (nonatomic, strong) NSMutableArray                    *filterVCArray;
@property (nonatomic, strong) NSMutableArray                    *stateVCArray;
@property (nonatomic, assign) NSInteger lastIndex;
@property (nonatomic, assign) BOOL selectTime;
@end

@implementation MSMyFollowViewController

- (void)initNavigationBar{
    [super initNavigationBar];
    leftBarBtn.hidden = NO;
    if (self.projectType != MSProjectType_Other) {
        titleLable.text = @"我跟踪的";
    }
    else{
        titleLable.text = @"其他项目";
    }
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    if (self.projectType != MSProjectType_CreatProject) {
        if ([self.filterVC.view superview]) {
            [self.filterVC dismiss];
        }
        if (!_selectTimeView.hidden) {//存在选择时间控件
            _selectTimeView.hidden = YES;
        }
        if ([self.areaFilterVC.view superview]) {
            [self.areaFilterVC dismiss];
        }
        [self.menu setBtnStateWithIndex:_lastIndex state:NO];
    }
}

- (void)initUI{
    self.tableVC = [[MSMyFollowTableVC alloc] initWithStyle:UITableViewStyleGrouped];
    self.tableVC.view.frame =  CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-KNavBarHeight);
    self.tableVC.enableRefresh = YES;
    self.tableVC.enableRequest = YES;
    self.tableVC.delegate = (id)self;
    self.tableVC.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:self.tableVC.view];
    
    documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) firstObject];
    fileManager = [NSFileManager defaultManager];
    currentProviceStr = @"";
    currentCityStr = @"";
    currentIndustryStr = @"";
    currentStateStr = @"";
    currentInvetStr = @"";
    currentTimeStr = @"";
    
    if (self.projectType != MSProjectType_CreatProject) {
        [self layOutHeaderView];
        self.tableVC.view.frame = CGRectMake(0, 40, SCREEN_WIDTH, SCREEN_HEIGHT-KNavBarHeight-40);
    }
}

- (void)layOutHeaderView{
    _positionY = 40;
    
    NSArray *titleArr = [NSArray arrayWithObjects:@"地区",@"行业",@"阶段",@"投资",@"时间", nil];
    self.menu = [[HDImageMenuView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40) titleArray:titleArr];
    self.menu.delegate = (id)self;
    self.menu.isHiddenBottomLine = NO;
    [self.contentView addSubview:self.menu];
    
    _lastIndex = -1;
    self.industryFilterVC = [self creatFilterVC];
    self.stateFilterVC = [self creatFilterVC];
    self.investFilterVC = [self creatFilterVC];
    self.timeFilterVC = [self creatFilterVC];
    
    self.filterVCArray = [NSMutableArray arrayWithObjects:self.areaFilterVC,self.industryFilterVC,self.stateFilterVC,self.investFilterVC,self.timeFilterVC, nil];
    
//    self.industryFilterVC.dataArray = [@[@"推荐",@"电力", @"能源矿产", @"冶金", @"化工医疗", @"轻纺食品", @"环保", @"市政公用", @"交通运输", @"机械电子", @"建筑", @"建材", @"农林水利", @"生态休闲旅游", @"其他"] mutableCopy];
//    self.stateFilterVC.dataArray = [@[@"推荐",@"工程筹备", @"勘察设计", @"主体施工", @"设备安装", @"装饰装修", @"园林绿化", @"竣工测试"] mutableCopy];
//    self.investFilterVC.dataArray = [@[@"不限",@"5000万以下",@"5000万-1亿",@"1亿-5亿",@"3000万-8000万",@"5亿-10亿",@"10亿以上"] mutableCopy];
//    self.timeFilterVC.dataArray = [@[@"不限",@"最近1个月",@"最近3个月",@"最近半年",@"最近1年",@"自定义"] mutableCopy];
//    
//    self.industryFilterVC.stateArray = [self creatStateArrayWithArray:self.industryFilterVC.dataArray];
//    self.stateFilterVC.stateArray = [self creatStateArrayWithArray:self.stateFilterVC.dataArray];
//    self.investFilterVC.stateArray = [self creatStateArrayWithArray:self.investFilterVC.dataArray];
//    self.timeFilterVC.stateArray = [self creatStateArrayWithArray:self.timeFilterVC.dataArray];
    
//    self.stateVCArray = [NSMutableArray arrayWithObjects:self.areaFilterVC,self.industryFilterVC.stateArray,self.stateFilterVC.stateArray,self.investFilterVC.stateArray,self.timeFilterVC.stateArray, nil];
    
//    self.areaFilterVC.dataArray = @[@{@"浙江":@[@"不限",@"非常诚意",@"诚意",@"一般",@"捣糨糊"]},@{@"湖北":@[@"不限",@"未报备",@"已报备",@"看过房",@"认筹过",@"认购过",@"签约过"]}];
    
    _selectTimeView = [[MSSelectTimeView alloc] initWithFrame:CGRectMake(0, 224, SCREEN_WIDTH, SCREEN_HEIGHT-224)];
    _selectTimeView.hidden = YES;
    _selectTimeView.delegate = (id)self;
    UIWindow *window = [UIApplication sharedApplication].windows[0];
    [window addSubview:_selectTimeView];
    
    [self fillFilterData];
}

- (NSMutableArray *)creatStateArrayWithArray:(NSArray*)array{
    NSMutableArray *mutalArray = [[NSMutableArray alloc] initWithCapacity:0];
    for (int i= 0; i<array.count; i++) {
        [mutalArray addObject:@"NO"];
    }
    return mutalArray;
}

- (void)fillFilterData{
    NSString *provincesFile = [documentsPath stringByAppendingPathComponent:@"provincesFile.plist"];
    if ([fileManager fileExistsAtPath:provincesFile]) {
        netEngine.provincesArray = [[NSMutableArray alloc] initWithContentsOfFile:provincesFile];
    }
    //省市
    NSMutableArray *tempDataArray = [[NSMutableArray alloc] initWithCapacity:0];
    NSMutableArray *tempCodeArray = [[NSMutableArray alloc] initWithCapacity:0];
    for (NSDictionary *dict in netEngine.provincesArray) {
        if ([dict isKindOfClass:[NSDictionary class]]) {
            NSArray *tempArray = [HDUtil checkArray:dict[@"citys"]];
            NSMutableArray *citysArray = [NSMutableArray arrayWithCapacity:0];
            NSMutableArray *citysCodeArray = [NSMutableArray arrayWithCapacity:0];
            for (NSDictionary *provinceDict in tempArray) {
                if ([provinceDict isKindOfClass:[NSDictionary class]]){
                    [citysArray addObject:[HDUtil check:provinceDict[@"name"]]];
                    [citysCodeArray addObject:[HDUtil check:provinceDict[@"code"]]];
                }
            }
            NSDictionary *chinaDict = @{[HDUtil check:dict[@"name"]]:citysArray};
            NSDictionary *chinaCodeDict = @{[HDUtil check:dict[@"code"]]:citysCodeArray};
            [tempDataArray addObject:chinaDict];
            [tempCodeArray addObject:chinaCodeDict];
        }
    }
    self.areaFilterVC.codeArray = tempCodeArray;
    self.areaFilterVC.dataArray = tempDataArray;
    // @[@{@"浙江":@[@"温州",@"舟山",@"宁波",@"杭州",@"德州",@"奉化",@"瑞安",@"乐清",@"海宁"]},@{@"湖北":@[@"武汉",@"襄阳",@"荆州",@"荆门",@"宜昌",@"黄冈",@"咸宁",@"随州"]}];
    
    //行业
    NSMutableArray *tempDataArray2 = [[NSMutableArray alloc] initWithCapacity:0];
    NSMutableArray *tempCodeArray2 = [[NSMutableArray alloc] initWithCapacity:0];
    
    NSString *projectIndustryFile = [documentsPath stringByAppendingPathComponent:@"projectIndustryFile.plist"];
    if ([fileManager fileExistsAtPath:projectIndustryFile]) {
        netEngine.projectIndustryArray = [[NSMutableArray alloc] initWithContentsOfFile:projectIndustryFile];
    }
    for (NSDictionary *dict in netEngine.projectIndustryArray) {
        if ([dict isKindOfClass:[NSDictionary class]]) {
            [tempDataArray2 addObject:[HDUtil check:dict[@"name"]]];
            [tempCodeArray2 addObject:[HDUtil check:dict[@"code"]]];
        }
    }
    self.industryFilterVC.dataArray = tempDataArray2;
    self.industryFilterVC.codeArray = tempCodeArray2;
    
    //阶段
    NSMutableArray *tempDataArray3 = [[NSMutableArray alloc] initWithCapacity:0];
    NSMutableArray *tempCodeArray3 = [[NSMutableArray alloc] initWithCapacity:0];
    NSString *projectStateFile = [documentsPath stringByAppendingPathComponent:@"projectStateFile.plist"];
    if ([fileManager fileExistsAtPath:projectStateFile]) {
        netEngine.projectStateArray = [[NSMutableArray alloc] initWithContentsOfFile:projectStateFile];
    }
    for (NSDictionary *dict in netEngine.projectStateArray) {
        if ([dict isKindOfClass:[NSDictionary class]]) {
            [tempDataArray3 addObject:[HDUtil check:dict[@"name"]]];
            [tempCodeArray3 addObject:[HDUtil check:dict[@"code"]]];
        }
    }
    self.stateFilterVC.dataArray = tempDataArray3;
    self.stateFilterVC.codeArray = tempCodeArray3;
    
    //投资
    NSMutableArray *tempDataArray4 = [[NSMutableArray alloc] initWithCapacity:0];
    NSMutableArray *tempCodeArray4 = [[NSMutableArray alloc] initWithCapacity:0];
    NSString *projectInvestmentFile = [documentsPath stringByAppendingPathComponent:@"projectInvestmentFile.plist"];
    if ([fileManager fileExistsAtPath:projectInvestmentFile]) {
        netEngine.projectInvestmentArray = [[NSMutableArray alloc] initWithContentsOfFile:projectInvestmentFile];
    }
    for (NSDictionary *dict in netEngine.projectInvestmentArray) {
        if ([dict isKindOfClass:[NSDictionary class]]) {
            [tempDataArray4 addObject:[HDUtil check:dict[@"name"]]];
            [tempCodeArray4 addObject:[HDUtil check:dict[@"code"]]];
        }
    }
    self.investFilterVC.dataArray = tempDataArray4;
    self.investFilterVC.codeArray = tempCodeArray4;
    
    //时间
    NSMutableArray *tempDataArray5 = [[NSMutableArray alloc] initWithCapacity:0];
    NSMutableArray *tempCodeArray5 = [[NSMutableArray alloc] initWithCapacity:0];
    NSString *projectTimeFile = [documentsPath stringByAppendingPathComponent:@"projectTimeFile.plist"];
    if ([fileManager fileExistsAtPath:projectTimeFile]) {
        netEngine.projectTimeArray = [[NSMutableArray alloc] initWithContentsOfFile:projectTimeFile];
    }
    for (NSDictionary *dict in netEngine.projectTimeArray) {
        if ([dict isKindOfClass:[NSDictionary class]]) {
            [tempDataArray5 addObject:[HDUtil check:dict[@"name"]]];
            [tempCodeArray5 addObject:[HDUtil check:dict[@"code"]]];
        }
    }
    self.timeFilterVC.dataArray = tempDataArray5;
    self.timeFilterVC.codeArray = tempCodeArray5;
    //    self.stateFilterVC.dataArray = [netEngine.projectStateArray mutableCopy];
    //    self.investFilterVC.dataArray = [netEngine.projectInvestmentArray mutableCopy];
    //    self.timeFilterVC.dataArray = [netEngine.projectTimeArray mutableCopy];
    
    //    self.industryFilterVC.dataArray = [@[@"推荐",@"电力", @"能源矿产", @"冶金", @"化工医疗", @"轻纺食品", @"环保", @"市政公用", @"交通运输", @"机械电子", @"建筑", @"建材", @"农林水利", @"生态休闲旅游", @"其他"] mutableCopy];
    //    self.stateFilterVC.dataArray = [@[@"推荐",@"工程筹备", @"勘察设计", @"主体施工", @"设备安装", @"装饰装修", @"园林绿化", @"竣工测试"] mutableCopy];
    //    self.investFilterVC.dataArray = [@[@"不限",@"5000万以下",@"5000万-1亿",@"1亿-5亿",@"3000万-8000万",@"5亿-10亿",@"10亿以上"] mutableCopy];
    //    self.timeFilterVC.dataArray = [@[@"不限",@"最近1个月",@"最近3个月",@"最近半年",@"最近1年",@"自定义"] mutableCopy];
    
    self.industryFilterVC.stateArray = [self creatStateArrayWithArray:self.industryFilterVC.dataArray];
    self.stateFilterVC.stateArray = [self creatStateArrayWithArray:self.stateFilterVC.dataArray];
    self.investFilterVC.stateArray = [self creatStateArrayWithArray:self.investFilterVC.dataArray];
    self.timeFilterVC.stateArray = [self creatStateArrayWithArray:self.timeFilterVC.dataArray];
    
    self.stateVCArray = [NSMutableArray arrayWithObjects:self.areaFilterVC,self.industryFilterVC.stateArray,self.stateFilterVC.stateArray,self.investFilterVC.stateArray,self.timeFilterVC.stateArray, nil];
}

- (void)testDataFunc{
    MSProjectModel *model = [[MSProjectModel alloc] init];
    model.title = @"上海浦东世和空港中心商业住房项目";
    model.projectType =  @"房地产";
    model.projectState =  @"主体施工";
    model.investment =  @"2亿";
    model.address =  @"上海";
    model.time =  @"2016.10.08";
    model.introduction =  @"项目为四栋单独塔楼，一座宾馆，一做酒店式办公楼，地下部分主题为一个小楼盘，整个项目投资回报率较高";
    
    MSProjectModel *model1 = [[MSProjectModel alloc] init];
    model1.title = @"苏州永华置业有限公司 意境鸣镝西部仓库";
    model1.projectType =  @"酒店";
    model1.projectState =  @"主体施工";
    model1.investment =  @"3亿";
    model1.address =  @"苏州";
    model1.time =  @"2016.10.08";
    model1.introduction =  @"项目为四栋单独塔楼，一座宾馆。";
    
    MSProjectModel *model2 = [[MSProjectModel alloc] init];
    model2.title = @"中国光大国际有限公司深圳办事处";
    model2.projectType =  @"市政公用";
    model2.projectState =  @"主体施工";
    model2.investment =  @"6亿";
    model2.address =  @"成都";
    model2.time =  @"2016.9.15";
    model2.introduction =  @"项目为四栋单独塔楼，一座宾馆，一做酒店式办公楼，地下部分主题为一个小楼盘，整个项目投资回报率较高";
    
//    self.tableVC.dataArray = [@[model,model1,model2,model,model2,model1,model] mutableCopy];
    self.tableVC.pageNum = 1;
    [self request:NO];
}

- (void)updateViewController:(NSNotification *)note {
    NSDictionary *responseDic = note.userInfo;
    if (responseDic[Receiver]==self.VctrID) {
        if (responseDic[@"MyTrack"]!=nil){
            if (responseDic[@"state"]!=nil){
                self.tableVC.totalPagesNum = [responseDic[@"totalPage"] intValue];
                if (self.tableVC.pageNum == 1) {
                    self.tableVC.dataArray = nil;
                }
                NSMutableArray *dataArrray = [[NSMutableArray alloc] initWithArray:self.tableVC.dataArray];
                [dataArrray addObjectsFromArray:responseDic[@"MyTrack"]];
                self.tableVC.dataArray = dataArrray;
            }
            else{
                self.tableVC.dataArray = self.tableVC.dataArray;
            }
        }
    }
}

#pragma mark DYTableViewProtocol
/** 上拉 */
- (void)pullTableView:(UITableView *)tableView isNextPage:(BOOL)isNextPage{
    self.tableVC.pageNum++;
    [self request:NO];
}

- (void)pullRefreshRequest
{
    self.tableVC.pageNum = 1;
    [self request:YES];
}

-(void)request:(BOOL)isPull
{
    UIView * hudView = isPull?nil:self.contentView;
    NSString *pageNumber = [NSString stringWithFormat:@"%d",self.tableVC.pageNum];
    NSDictionary *dict = @{@"provinces":currentProviceStr,
                           @"citys":currentCityStr,
                           @"industrys":currentIndustryStr,
                           @"stages":currentStateStr,
                           @"investment":currentInvetStr,
                           @"onlineTime":currentTimeStr,
                           @"kw":@"",
                           @"pageNo":pageNumber,
                           @"pageSize":@"10",
                           };
    if (self.projectType != MSProjectType_Other) {
        [netEngine requestMyTrackWithTarget:self.VctrID hudView:hudView params:dict];
    }
    else{
        [netEngine requestContactsOtherProjectWithTarget:self.VctrID hudView:self.contentView contactsId:_peopleId params:@{@"pageNo":pageNumber,@"pageSize":@"10"}];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.projectType == MSProjectType_CreatProject) {
        NSArray* viewControllers = [HDUtil checkNilArray:[rootNavigation viewControllers]];
        NSInteger selfIndex = [viewControllers indexOfObject:self];
        if (selfIndex-1>=0) {
            [viewControllers[selfIndex-1] removeFromParentViewController];
        }
        
        MSProjectModel *model = self.tableVC.dataArray[indexPath.row];
        [rootNavigation popViewControllerAnimated:YES];
        [[NSNotificationCenter defaultCenter] postNotificationName:KSelectedProject object:self userInfo:@{model.title:model.itemID}];
    }
    else{
        MSProjectDetailViewController *vc = [[MSProjectDetailViewController alloc] init];
        [rootNavigation pushViewController:vc animated:YES];
    }
}

- (void)selectedDate:(NSDate*)startDate end:(NSDate*)endDate{
    _selectTimeView.hidden = YES;
    _selectTime = YES;
    [self.menu setBtnStateWithIndex:_lastIndex state:NO];
    [self.menu setBtnTitleColorWithIndex:4 state:YES];
    [self.filterVC selectedTime];
}

#pragma mark - MSDoubleListFilterVCDelegate
- (void)selectedAreaProviceCode:(NSDictionary *)proviceDict cityCode:(NSDictionary *)cityDict isTap:(BOOL)isTap{//地区
    [self.menu setBtnStateWithIndex:_lastIndex state:NO];
    if (isTap) {
//        [self.menu setBtnStateWithIndex:0 state:NO];
        return;
    }
    NSString *cityName = [cityDict allKeys][0];
    if (cityName.length) {
        currentProviceStr = [proviceDict allValues][0];
        currentCityStr = [cityDict allValues][0];
        [self.menu setBtnTitleColorWithIndex:0 state:YES];
    }
    else{
        currentProviceStr = @"";
        currentCityStr = @"";
        [self.menu setBtnTitleColorWithIndex:0 state:NO];
    }
    
    self.tableVC.pageNum = 1;
    [self request:NO];
}

#pragma mark - MSSaleFilterDelegate
- (void)selectedFilterWithParmas:(NSInteger)index code:(NSString*)codeValue{
    if (_lastIndex == 4) {//时间
        if (index == 100 && !_selectTime) {//自定义时间
            _selectTimeView.hidden = NO;
            _selectTimeView.frame = CGRectMake(0, _positionY+KNavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-_positionY - KNavBarHeight);
            [_selectTimeView showInView:SCREEN_HEIGHT-_positionY - KNavBarHeight];
            UIWindow *window = [UIApplication sharedApplication].windows[0];
            [window bringSubviewToFront:_selectTimeView];
            return;
        }
        else{
            [self.menu setBtnTitleColorWithIndex:4 state:YES];
        }
    }
    _selectTime = NO;
    [self.menu setBtnStateWithIndex:_lastIndex state:NO];
    NSArray *stateArray = self.stateVCArray[_lastIndex];
    if ([stateArray containsObject:@"YES"]) {
        [self.menu setBtnTitleColorWithIndex:_lastIndex state:YES];
    }
    else{
        [self.menu setBtnTitleColorWithIndex:_lastIndex state:NO];
    }
    
    if (_lastIndex == 1) {//行业
        if ([stateArray containsObject:@"YES"]) {
            currentIndustryStr = codeValue;
        }
        else{
            currentIndustryStr = @"";
        }
    }
    if (_lastIndex == 2) {//阶段
        if ([stateArray containsObject:@"YES"]) {
            currentStateStr = codeValue;
        }
        else{
            currentStateStr = @"";
        }
    }
    if (_lastIndex == 3) {//投资
        if ([stateArray containsObject:@"YES"]) {
            currentInvetStr = codeValue;
        }
        else{
            currentInvetStr = @"";
        }
    }
    if (_lastIndex == 4) {//时间
        if ([stateArray containsObject:@"YES"]) {
            currentTimeStr = codeValue;
        }
        else{
            currentTimeStr = @"";
        }
    }
    if (index != 1000) {
        self.tableVC.pageNum = 1;
        [self request:NO];
    }
}

#pragma mark - HDMenuImageDelegate
- (void)selectImageMenuIndex:(int)index{
    if (!_selectTimeView.hidden) {//存在选择时间控件
        _selectTimeView.hidden = YES;
    }
    if (_lastIndex != -1) {//不是第一次
        [self.menu setBtnStateWithIndex:_lastIndex state:NO];
        if (index != 0) {//点击其它
            if (index != _lastIndex) {
                self.filterVC = self.filterVCArray[_lastIndex];
                if ([self.filterVC.view superview]) {
                    [self.filterVC dismiss];
                }
                self.filterVC = self.filterVCArray[index];
                if ([self.filterVC.view superview]) {
                    [self.filterVC dismiss];
                }
                else{
                    [self.filterVC showInView:_positionY];
                    [self.menu setBtnStateWithIndex:index state:YES];
                }
            }
            else{
                self.filterVC = self.filterVCArray[index];
                if ([self.filterVC.view superview]) {
                    [self.filterVC dismiss];
                }
                else{
                    [self.filterVC showInView:_positionY];
                    [self.menu setBtnStateWithIndex:index state:YES];
                }
            }
        }
        else{//点击地区
            if (_lastIndex != 0) {
                self.filterVC = self.filterVCArray[_lastIndex];
                if ([self.filterVC.view superview]) {
                    [self.filterVC dismiss];
                }
            }
            
            if ([self.areaFilterVC.view superview]) {
                [self.areaFilterVC dismiss];
                [self.menu setBtnStateWithIndex:index state:NO];
            } else {
                [self.areaFilterVC showInView:_positionY];
                [self.menu setBtnStateWithIndex:index state:YES];
            }
        }
    }
    else{//第一次点击
        if (index != 0) {//1
            if ([self.areaFilterVC.view superview]) {
                [self.areaFilterVC dismiss];
            } else {
                if (_lastIndex != -1 && _lastIndex != 0) {
                    self.filterVC = self.filterVCArray[_lastIndex];
                    if ([self.filterVC.view superview]) {
                        [self.filterVC dismiss];
                    } else {//旧的不在
                        self.filterVC = self.filterVCArray[index];
                        if ([self.filterVC.view superview]) {
                            [self.filterVC dismiss];
                        }
                        else{
                            [self.filterVC showInView:_positionY];
                            [self.menu setBtnStateWithIndex:index state:YES];
                        }
                    }
                }
                else{//第一次点击
                    self.filterVC = self.filterVCArray[index];
                    
                    if ([self.filterVC.view superview]) {
                        [self.filterVC dismiss];
                    }
                    else{
                        [self.filterVC showInView:_positionY];
                        [self.menu setBtnStateWithIndex:index state:YES];
                    }
                }
            }
        }
        else{//点击地区
            if ([self.areaFilterVC.view superview]) {
                [self.areaFilterVC dismiss];
                [self.menu setBtnStateWithIndex:index state:NO];
            } else {
                [self.areaFilterVC showInView:_positionY];
                [self.menu setBtnStateWithIndex:index state:YES];
            }
        }
    }
    _lastIndex = index;
}

- (MSSaleFilterViewController *)creatFilterVC
{
    MSSaleFilterViewController *VC = [[MSSaleFilterViewController alloc] init];
    VC.delegate = (id<MSSaleFilterDelegate>)self;
    return VC;
}

- (MSDoubleListFilterVC *)areaFilterVC
{
    if (!_areaFilterVC) {
        _areaFilterVC = [[MSDoubleListFilterVC alloc] init];
        _areaFilterVC.delegate=(id<MSDoubleListFilterVCDelegate>)self;
    }
    return _areaFilterVC;
}

#pragma mark - scrollviewBeginDragging
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    if ([self.filterVC.view superview]) {
        [self.filterVC dismiss];
    }
    if (!_selectTimeView.hidden) {//存在选择时间控件
        _selectTimeView.hidden = YES;
    }
    if ([self.areaFilterVC.view superview]) {
        [self.areaFilterVC dismiss];
    }
    [self.menu setBtnStateWithIndex:_lastIndex state:NO];
}

@end
